<?php

defined('ACC')||exit('Access Denied');

class FlowModel extends Model{
	
	/* 获取地址信息 */
	public function getRegions($region_id){
		$sql = 'select region_id, parent_id, region_name from region where parent_id = ' . $region_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取用户地址 */
	public function getUserAddress($user_id){
		$sql = 'select * from user_address where user_id = ' . $user_id;
		return $this->db->getAll($sql);
	}
	
	/* 修改用户地址 */
	public function regionInsert($data, $table, $user_id){
		$sql = 'select address_id from user_address limit 0,1';
		if($this->db->getOne($sql)){
			$sql = 'update user_address set is_apply = 0 where user_id = ' . $user_id;
			$this->db->query($sql);
		}
		return $this->db->autoExecute($data, $table);
	}
	
	/* 获取用户所有收件地址 */
	public function getConsignee($user_id){
		$sql = 'select * from user_address where user_id = ' . $user_id . ' and is_apply = 1';
		return $this->db->getRow($sql);
	}
	
	/* 获取购物信息 */
	public function getShippings(){
		$sql = 'select shipping_id, shipping_name, shipping_desc, insure, price from shipping where support_cod = 1';
		return $this->db->getAll($sql);
	}
	
	/* 获取一个收件人地址 */
	public function get_address($address_id){
		$sql = 'select address_id, consignee, email, country, province, city, district, address , zipcode, tel, mobile from user_address where address_id = ' . $address_id;
		return $this->db->getRow($sql);
	}
	
	/* 获取一行商品信息 */
	public function get_good_info($goods_id){
		$sql = 'select goods_id, goods_name, market_price, shop_price from goods where goods_id = ' . $goods_id;
		return $this->db->getRow($sql);
	}
	
	/* 修改收件人地址 */
	public function addressUpdate($data, $table, $where){
		return $this->db->autoExecute($data, $table, 'update', $where);
	}
	
	/* 修改购物车价格 */
	public function get_shipping_price($shipping_id){
		$sql = 'select price from shipping where shipping_id = ' . $shipping_id;
		return $this->db->getOne($sql);
	}
	
	/* 获取用户地址 */
	public function get_user_address($address_id){
		$sql = 'select * from user_address where address_id = ' . $address_id;
		return $this->db->getRow($sql);
	}
	
	/* 增加一个订单 */
	public function add($data){
		if($this->db->autoExecute($data,'order_info')){
			return $this->db->last_id();
		}else{
			return false;
		}
	}
	
	/* 添加一个订单下所有商品  */
	public function addGoods($data){
		if($this->db->autoExecute($data,'order_goods')){
			return $this->db->last_id();
		}else{
			return false;
		}
	}
	
	/* 删除订单信息 */
	public function invoke($order_id){
		$sql='delete from order_info where order_id='.$order_id;
		$this->db->query($sql);
		$sql='delete from order_goods where order_id='.$order_id;
		$this->db->query($sql);
	}
	
}












